package com.linmo.chatmanage;

import com.linmo.questionmatcher.QuestionMatcher;
import com.linmo.util.ChatBotThread;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChatJPanelGUI extends JPanel {

    private static QuestionMatcher questionMatcher = new QuestionMatcher();
    private static ChatManage chatManage = new ChatManage();

    private int currentSessionId;

    private JTextArea chatArea;
    private JTextField inputField;
    private JButton sendButton;
    private JButton newSessionButton;

    public ChatJPanelGUI(int userId) {
        // 会话区实现
        setLayout(new BorderLayout());
        chatArea = new JTextArea();
        chatArea.setEditable(false);
        inputField = new JTextField();
        sendButton = new JButton("发送");
        newSessionButton = new JButton("新建会话");

        // 创建初始会话
        currentSessionId = chatManage.createSession(userId);

        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userInput = inputField.getText();
                if (!userInput.trim().isEmpty()) {
                    getAppend("User: " + userInput + "\n");

//                    String response = questionMatcher.matchResponse(currentSessionId, userInput);
//                    getAppend("Linmo: " + response + "\n");

                    // 在后台线程中处理问题匹配
                    ChatBotThread.execute(new Runnable() {
                        @Override
                        public void run() {
                            String response = questionMatcher.matchResponse(currentSessionId, userInput);

                            // 更新界面需要在主线程中进行
                            SwingUtilities.invokeLater(new Runnable() {
                                @Override
                                public void run() {getAppend("Linmo: " + response + "\n");
                                }
                            });
                        }
                    });

                    inputField.setText("");
                }
            }
        });

        newSessionButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                endCurrentSessionAndSave();
                clearChatArea();// 清空会话区
                //  新建会话
                currentSessionId = chatManage.createSession(userId);
            }
        });

        JPanel inputPanel = new JPanel(new BorderLayout());
        inputPanel.add(inputField, BorderLayout.CENTER);
        inputPanel.add(sendButton, BorderLayout.EAST);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 新增按钮面板
        buttonPanel.add(newSessionButton);

        add(new JScrollPane(chatArea), BorderLayout.CENTER);
        add(inputPanel, BorderLayout.SOUTH);
        add(buttonPanel, BorderLayout.NORTH); // 将按钮面板添加到顶部
    }

    public void endCurrentSessionAndSave() {
        //  调用回答生成模块的结束会话方法
        chatManage.endSession(currentSessionId);
    }

    public void getAppend(String str) {
        chatArea.append(str);
    }

    public void clearChatArea() {
        chatArea.setText("");
    }

    public void setCurrentSessionId(int sessionId) {
        this.currentSessionId = sessionId;
    }

    public void shutdown() {
        ChatBotThread.shutdown();
    }
}
